package com.ebupt.migu.music.system.controller;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.system.entity.SysRole;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.system.service.RoleService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@EnableAutoConfiguration
@Controller
@RequestMapping("/system/role")
public class RoleController {
	@Autowired
    HttpServletRequest request;//这样使用，request每次进来都是Current Request

	@Autowired
	RoleService roleService;

    @RequestMapping("/list")
    @ResponseBody
    public ResultObject<Object> list() {
    	ResultObject<Object> retObj = new ResultObject<Object>();
    	Map<String,Object> paramMap = ReqestParamUtil.getParams(request);

    	retObj.setData(roleService.queryByPage(paramMap));

    	return retObj;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultObject<Object> add() {

    	ResultObject<Object> retObj = new ResultObject<Object>();
		Map<String,Object> retMap = new HashMap<>();
        // 获取用户信息和业务参数
        Map<String, Object> paramMap = AuthBaseUtils.authBaseInfo(request);

        /*
        // 获取header的参数
		String loginMessage = ReqestParamUtil.getHeader(request, "loginMessage");
		Gson gson = new Gson();
		Map<String,Object> loginMessageMap = gson.fromJson(loginMessage, Map.class);

		// 判断header 是否为空
		if (loginMessageMap == null || loginMessageMap.size() == 0) {
			throw new MyException(StatusEnum.HEADER_ERROR, "");
		}

		Object loginId = loginMessageMap.get("loginId");

		// 判断loginId是否为空
		if (loginId == null) {
			throw new MyException(StatusEnum.USER_VERIFICATION_FAILED, "");
		}
		// 添加创建者参数
		paramMap.put("creator", paramMap.get("").toString());
		*/



		// 验证角色是否重名
		String roleName = paramMap.get("roleName").toString();
		Map<String, Object> dupRoleNameMap = new HashMap<>();
		dupRoleNameMap.put("roleName", roleName);
		dupRoleNameMap.put("pageStart", 0);
		dupRoleNameMap.put("rows", 9999);
		Page<SysRole> page = roleService.queryByPage(dupRoleNameMap);
		List<SysRole> roleList = page.getData();
		if (roleList != null) {
		    for (SysRole role : roleList) {
				if (roleName.equals(role.getName())) {
					throw new MyException(StatusEnum.ROLE_NAME_EXISTS_ERROR, "");
				}
            }
        }

        String roleId = roleService.addRole(paramMap);

        retMap.put("roleId", roleId);
        retObj.setData(retMap);
        retObj.setMsg("新增角色成功");

    	return retObj;
    }

    @RequestMapping("/modify")
    @ResponseBody
    public ResultObject<Object> modify() {
    	ResultObject<Object> retObj = new ResultObject<Object>();
		Map<String,Object> retMap = new HashMap<>();
    	Map<String,Object> paramMap = ReqestParamUtil.getParams(request);

    	String roleId = roleService.modifyRole(paramMap);

		retMap.put("roleId", roleId);
    	retObj.setData(retMap);
		retObj.setMsg("修改角色成功");

    	return retObj;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultObject<Object> delete() {
    	ResultObject<Object> retObj = new ResultObject<Object>();
    	Map<String,Object> paramMap = ReqestParamUtil.getParams(request);

		String roleId = paramMap.get("roleId").toString();

		if ("0".equals(roleId)) {
			throw new MyException(StatusEnum.DELETE_SUPER_ADMIN_ROLE_ERROR, "");
		} else if("9999".equals(roleId) || "8888".equals(roleId)){
			throw new MyException(StatusEnum.DELETE_AUDIT_ROLE_ERROR, "");
		} else {

			List<SysUser> roleUserList = roleService.queryRoleUserById(roleId);

			if (roleUserList != null && roleUserList.size() > 0) {
				String users = "";
				for (SysUser user : roleUserList) {
					users += "[" + user.getId() + ":" + user.getUserName() + "],";
				}
				users = users.substring(0, users.length() - 1);

				throw new MyException(StatusEnum.ROLE_USER_NOT_ZERO, "使用该角色的用户"+users);
			}

			roleService.delRole(paramMap);
			retObj.setMsg("删除角色成功");
		}

    	return retObj;
    }

    @RequestMapping("/rolePrivList")
	@ResponseBody
	public ResultObject<Object> rolePrivList() {
		ResultObject<Object> retObj = new ResultObject<Object>();
		Map<String,Object> paramMap = ReqestParamUtil.getParams(request);

		retObj.setData(roleService.getRolePrivList(paramMap));
		retObj.setMsg("查询角色权限成功");
		return retObj;
	}

}